package com.molichuxing.services.sdk.gzt;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpStatus;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.molichuxing.framework.utils.HttpClientUtil;
import com.molichuxing.services.util.MD5Util;

import cn.id5.gboss.GbossClient;
import cn.id5.gboss.GbossConfig;
import cn.id5.gboss.http.HttpResponseData;
@Component
public class GztUtil {

    private static Logger loger = Logger.getLogger(GztUtil.class);
    
    private static String identityAccessId;
    
    private static String identityAccessPwd;
    
    private static String desCharset;
    
    private static String desKey;

    private static String verifyThreeProductNum;
    
    private static String drivingLicenseProductNum;
           
    private static String webserviceId;
            
    private static String webservicePwd;
            
    private static String verifyThreeUrl;
    
    private static String verifyIdentityUrl;

    /**
     * 
     * @param realName 真实姓名
     * @param idNumber 身份证号
     * @param faceImg  识别图像 Base64
     * @return
     * @throws Exception
     */
    public static String verifyIdentity(String realName, String idNumber, String faceImg) throws Exception {
    	SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String timeStamp=simpleDateFormat.format(new Date());
        String accessKey = MD5Util.MD5(identityAccessId + identityAccessPwd + timeStamp);
        realName=java.net.URLEncoder.encode(realName,"UTF-8");
        Map<String, String> paramMap=new HashMap<String, String>(6);
        paramMap.put("accessId", identityAccessId);
        paramMap.put("accessKey", accessKey);
        paramMap.put("timeStamp", timeStamp);
        paramMap.put("userId", idNumber);
        paramMap.put("userName", realName);
        paramMap.put("faceImg", faceImg);
        return HttpClientUtil.doPost(verifyIdentityUrl, paramMap);
    }


    //初始化一次
    private static GbossClient client ;
    
    public static GbossClient getClient() {
		if (client == null) {
			client = new GbossClient(getConfig());
		}
		return client;
	}
    /**
     * 国政通三要素验证
     * @param idNumber   身份证号码
     * @param realName   姓名
     * @param phoneNumber  手机号码
     * @return
     * @throws Exception
     */
    public static String verifyThreefactor(String idNumber,String realName,String phoneNumber) throws Exception{
    	 String param =phoneNumber+","+realName+","+idNumber;
         HttpResponseData httpdata = client.invokeSingle(verifyThreeProductNum, param);
         loger.info("gboss 调用耗时  = " + httpdata.getTime());
         if(httpdata.getStatus()==HttpStatus.SC_OK){
           return httpdata.getData();
         }   
         return null;
    }

    /**
     * 国政通驾驶证
     * @param idNumber   身份证号码
     * @param realName   姓名
     * @return
     * @throws Exception
     */
    public static String verifyDrivingLicense(String realName,String idNumber) throws Exception{
    	//String param =phoneNumber+","+fullName+","+idNumber;
        String randomNo = new Date().getTime()+"";
        String param =randomNo+","+realName+","+idNumber;
        HttpResponseData httpdata = client.invokeSingle(drivingLicenseProductNum, param);
        loger.info("gboss 调用耗时  = " + httpdata.getTime());
        if(httpdata.getStatus()==HttpStatus.SC_OK){
            return httpdata.getData();
        }  
        return null;
    }

    private static GbossConfig getConfig() {

        GbossConfig config = new GbossConfig();
        config.setEndpoint(verifyThreeUrl);
        config.setDesKey(desKey);
        config.setEncrypted(true);
        //分配帐号
        config.setAccount(webserviceId);
        //分配密码
        config.setAccountpwd(webservicePwd);
        config.setDesCharset(desCharset);
        config.setTimeout(10000);
        return config;

    }

    @Value("${gzt.identity.access.id}")
    public void setIdentityAccessId(String identityAccessId) {
    	GztUtil.identityAccessId=identityAccessId;
    }
    @Value("${gzt.identity.access.pwd}")
    public void setIdentityAccessPwd(String identityAccessPwd) {
    	GztUtil.identityAccessPwd=identityAccessPwd;
    }
    @Value("${gzt.verify.three.product.num}")
    public void setVerifyThreeProductNum(String verifyThreeProductNum) {
    	GztUtil.verifyThreeProductNum=verifyThreeProductNum;
    }
    @Value("${gzt.drivingLicense.product.num}")
    public void setDrivingLicenseProductNum(String drivingLicenseProductNum) {
    	GztUtil.drivingLicenseProductNum=drivingLicenseProductNum;
    }
    @Value("${gzt.webservice.id}")
    public void setWebserviceId(String webserviceId) {
    	GztUtil.webserviceId=webserviceId;
    }
    @Value("${gzt.webservice.pwd}")
    public void setWebservicePwd(String webservicePwd) {
    	GztUtil.webservicePwd=webservicePwd;
    }
    @Value("${gzt.verify.three.url}")
    public void setVerifyThreeUrl(String verifyThreeUrl) {
    	GztUtil.verifyThreeUrl=verifyThreeUrl;
    }
    @Value("${gzt.des.charset}")
    public void setDesCharset(String desCharset) {
    	GztUtil.desCharset=desCharset;
    }
    @Value("${gzt.des.key}")
    public void setDesKey(String desKey) {
    	GztUtil.desKey=desKey;
    }
    @Value("${gzt.verify.identity.url}")
    public void setVerifyIdentityUrl(String verifyIdentityUrl) {
    	GztUtil.verifyIdentityUrl=verifyIdentityUrl;
    }

}

